<?php

namespace Cekurte\Logging;

/**
 * Escreve loggers dos comandos SQL que estão sendo executados gravando-os em arquivos físicos.
 * 
 * @author  João Paulo Cercal
 * @version	1.0
 */
class Sql implements \Doctrine\DBAL\Logging\SQLLogger
{	
	private static $logger;
	
	public function __construct()
	{
		self::$logger 	= array(
			'sql'		=> null,
			'params'	=> null,
			'types'		=> null,
		);
	}
	
    /**
     * {@inheritdoc}
     */
    public function startQuery($sql, array $params = null, array $types = null)
    {
    	self::$logger 	= array(
			'sql'		=> $sql,
			'params'	=> $params,
			'types'		=> $types,
		);
    	
    	$writer 	= new \Zend_Log_Writer_Stream( APPLICATION_PATH . '/data/logs/sql.log' );
    	
		$logger 	= new \Zend_Log($writer);
		
		$datetime	= new \Zend_Date();
		
		$datetime	= $datetime->toString( 'YYYY-MM-dd HH:mm:ss' );
		
		$formatter 	= new \Zend_Log_Formatter_Simple("{$datetime}: " . '%message%' . PHP_EOL);

		$writer->setFormatter( $formatter );

		$logger->log( $sql , \Zend_Log::DEBUG );
    }

    /**
     * {@inheritdoc}
     */
    public function stopQuery()
    {
    	/*
    	if( APPLICATION_ENV === 'development' )
    	{
    		$sql = self::$logger['sql'];
    		
    		echo '<div class="logger">'
    			. "<span class='sql'>{$sql}</span>"
    			. '</div>'
    		;
    		
    	}
    	*/
    }
}